package org.klomp.snark.utilities;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;

public class Utilities {
	public static long time = 0;
	private static boolean redownloadAllowed = false;
	
	public static boolean redownloadAllowed() {
		return redownloadAllowed;
	}
	public static void setRedownloadAllowed(boolean allowed) {
		redownloadAllowed = allowed;
	}
	
    public static TimeUnitsPair getUnits(long time) {   
    	if(time >= 1000) {
    		time /= 1000; // seconds
    		if(time >= 60) {
    			time /= 60; // minutes
    			if(time >= 60) {
    				time /= 60; // hours
    				if(time >= 24) {
    					return new TimeUnitsPair(time, "hours (more than a day)");
    				}
    				else {
    					return new TimeUnitsPair(time, "hr");   
    				}
    			}
    			else {
    				return new TimeUnitsPair(time, "min");    
    			}
    		}
    		else {
    			return new TimeUnitsPair(time, "s");
    		}
    	}
    	else {
    		return new TimeUnitsPair(time, "ms");
    	}    	 
    }
    
    public static void printCurrentTime() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(cal.getTime()));
    }
    
    /**
     * Simulates a coin flip
     * 
     * Heads (true) is < 0.5d & > 0.0d
     * Tails (false) is >= 0.5d & < 1.0d
     * 
     * @return
     */
    public static boolean coinFlip() {
    	return Math.random() < 0.5d;
    }
    
    /**
     * Return a random number between 0 and upper - 1
     * 
     * @param int upper
     * @return int
     */
    public static int random(int upper) {
    	Random r = new Random();    	
    	int rand = r.nextInt(upper);
    	return rand;
    }
    
    public static boolean deleteDirectory(File path) {
    	if(path.exists()) {
    		File[] files = path.listFiles();
    		for(int i=0; i<files.length; i++) {
    			if(files[i].isDirectory()) {
    				deleteDirectory(files[i]);
    			}
    			else {
    				files[i].delete();
    			}
    		}
    	}
    	return(path.delete());
    }
    
    public static void main(String[] args) {
    	System.out.println(random(5));
    }
}
